import { ref } from 'vue'
import * as Api from '@/api/user/useManagement';
import * as T from '@/api/user/userManagement_type';

export const formData = ref<T.TUserItem>(T.defaultUserItem());

export const UserRuls:Record<string, any |any[]> = {
  account: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  account_status: [
    { required: true, message: '请选择账号状态', trigger: 'change' }
  ],
  org_code: [
    { required: true, message: '请选择机构', trigger: 'change' }
  ],
  username: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
    { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3456789]\d{9}$/, message: '手机号格式错误', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '邮箱格式错误', trigger: 'blur' }
  ],
  passwd: [
    // { required: true, message: '请输入密码', trigger: 'blur' },
    // { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
    // 自定义验证
    { validator: (value: string, callback: (arg0: any) => void) => {
      let message = ''
      if(!value) {
        message = '请输入密码'
      }
      if(value.length < 6) {
        message = '密码长度不能小于6位'
      }
      if(value.length > 20) {
        message = '密码长度不能大于20位'
      }
      callback(message)
    }, trigger: 'blur' }
  ]
}
// 递归将树的全部节点展成一维数组
export function getTreeData(tree: any[], arr: any[] = []) {
  tree.forEach(item => {
    arr.push(item)
    if(item.children && item.children.length) {
      getTreeData(item.children, arr)
    }
  })
  return arr
}